//
//  KiwipenRecord.m
//  SqliteManagerDemo
//
//  Created by US bike on 2017/9/5.
//  Copyright © 2017年 US bike. All rights reserved.
//

#import "KiwipenRecord.h"

@implementation KiwipenRecord



+ (NSData *) addHeaderSign:(NSData *) fileData {
    
    long totalAudioLen ;
    long totalDataLen ;
    long longSampleRate = 8000;
    int channels = 2;
    long byteRate = 16 * 8000 * channels / 8;
    totalAudioLen = fileData.length;
    totalDataLen = totalAudioLen + 36;

    
    Byte *header = malloc(sizeof(Byte) * 44);
    header[0] = 'R'; // RIFF/WAVE header
    header[1] = 'I';
    header[2] = 'F';
    header[3] = 'F';
    header[4] = (Byte) (totalDataLen & 0xff);
    header[5] = (Byte) ((totalDataLen >> 8) & 0xff);
    header[6] = (Byte) ((totalDataLen >> 16) & 0xff);
    header[7] = (Byte) ((totalDataLen >> 24) & 0xff);
    header[8] = 'W';
    header[9] = 'A';
    header[10] = 'V';
    header[11] = 'E';
    header[12] = 'f'; // 'fmt ' chunk
    header[13] = 'm';
    header[14] = 't';
    header[15] = ' ';
    header[16] = 16; // 4 bytes: size of 'fmt ' chunk
    header[17] = 0;
    header[18] = 0;
    header[19] = 0;
    header[20] = 1; // format = 1
    header[21] = 0;
    header[22] = (Byte) channels;
    header[23] = 0;
    header[24] = (Byte) (longSampleRate & 0xff);
    header[25] = (Byte) ((longSampleRate >> 8) & 0xff);
    header[26] = (Byte) ((longSampleRate >> 16) & 0xff);
    header[27] = (Byte) ((longSampleRate >> 24) & 0xff);
    header[28] = (Byte) (byteRate & 0xff);
    header[29] = (Byte) ((byteRate >> 8) & 0xff);
    header[30] = (Byte) ((byteRate >> 16) & 0xff);
    header[31] = (Byte) ((byteRate >> 24) & 0xff);
    header[32] = (Byte) (2 * 16 / 8); // block align
    header[33] = 0;
    header[34] = 16; // bits per sample
    header[35] = 0;
    header[36] = 'd';
    header[37] = 'a';
    header[38] = 't';
    header[39] = 'a';
    header[40] = (Byte) (totalAudioLen & 0xff);
    header[41] = (Byte) ((totalAudioLen >> 8) & 0xff);
    header[42] = (Byte) ((totalAudioLen >> 16) & 0xff);
    header[43] = (Byte) ((totalAudioLen >> 24) & 0xff);
    
    
    NSData *headerData = [NSData dataWithBytes:header length:44];
    
    NSLog(@"headerData = %@", headerData);

    NSMutableData *resultData = [NSMutableData dataWithData:headerData];
    [resultData appendData:fileData];
    
    return resultData;
}


+ (instancetype) createTestRecord {
    //NSDateFommat 消耗时间较多， 建议设置为单例，
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    //[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];//东八区
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
    
    KiwipenRecord *record = [KiwipenRecord new];
    //record.recordID = [@(arc4random() % 1000) stringValue];//自动生成才对
    record.userID = @"1000001";
    record.deviceID = @"FFFFFFFFFFFF";
    record.roleType = 2;
    record.originLanguage = @"zh_CH";
    record.transformLuanguage = @"en_EN";
    record.originText = @"你好吗？";
    record.transformText = @"How are you?";
    record.updateTime = [formatter stringFromDate:[NSDate date]];
    record.originVideoPath = @"/video/originVideoTest.wav";
    record.transformVideoPath = @"/video/transforVideoTest.wav";
    
    return record;
}


+ (instancetype) createRecordWithUserId:(NSString *) userID DeviceID:(NSString *) deviceID RoleType:(RoleType) roleType  {
    //NSDateFommat 消耗时间较多， 建议设置为单例，
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    //[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];//东八区
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
    
    KiwipenRecord *record = [KiwipenRecord new];
    //record.recordID = [@(arc4random() % 1000) stringValue];//自动生成才对
    record.userID = @"1000001";
    record.deviceID = @"FFFFFFFFFFFF";
    record.roleType = 2;
    record.originLanguage = @"zh_CH";
    record.transformLuanguage = @"en_EN";
    record.originText = @"你好吗？";
    record.transformText = @"How are you?";
    record.updateTime = [formatter stringFromDate:[NSDate date]];
    record.originVideoPath = [NSString stringWithFormat:@"/video/%@/origin.wav",record.updateTime];
    record.transformVideoPath = [NSString stringWithFormat:@"/video/%@/transfor.wav",record.updateTime];
    
    return record;
}


@end
